package com.antdesignbackend.backend.webAPP;

import com.antdesignbackend.backend.common.returnResult.ReturnCode;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.Button;
import com.antdesignbackend.backend.entity.Immune;
import com.antdesignbackend.backend.entity.entityVo.ImmuneVo;
import com.antdesignbackend.backend.service.ImmuneService;
import com.antdesignbackend.backend.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/immuneAPP")
public class ImmuneAPPController{
    @Autowired
    ImmuneService immuneService;

    @Autowired
    PermissionService permissionService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private Integer menuId;

    @GetMapping("/index")
    public String ProductionIndex(String menuId, Model model) {
//        model.addAttribute("animalTypes",getLoginAnimalTypes());
//        this.menuId = Integer.parseInt(menuId);
        return "records/immune/index";
    }
    /**
     * 分页查询
     * @param immuneVo
     * @return
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody ImmuneVo immuneVo, HttpServletRequest request) {
        ReturnVO result = new ReturnVO();
        List<ImmuneVo> datalist=new ArrayList<>();
        try {
            QueryWrapper<ImmuneVo> queryWrapper = new QueryWrapper<>();
            if (immuneVo.getVaccineName() != null && !immuneVo.getVaccineName().equals("")){
                queryWrapper.like("vaccineName",immuneVo.getVaccineName());
            }
            if (immuneVo.getTimeInterval() != null && !immuneVo.getTimeInterval().equals("")){
                String[] str=immuneVo.getTimeInterval().split(" - ");
                String star=str[0];
                String end=str[1];
                queryWrapper.between("immuneTime",star,end);
            }
            queryWrapper.orderByDesc("immuneTime").orderByDesc("createTime");
            queryWrapper.eq("eid",immuneVo.getEid());
            //先统计满足条件的数据总数，因为sql 不包括排序
            int total=immuneService.count(queryWrapper);
            result.setTotal(total);
            //再查询
            if(total>0)
            {
                datalist= immuneService.search(queryWrapper,new Page<>(immuneVo.getPageIndex(),immuneVo.getPageSize()));
            }
            result.setData(datalist);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 添加
     * @param immune
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ReturnVO add(@RequestBody Immune immune) {
        try {
            immune.setUid(immune.getUid());
            immune.setCreateTime(new Date());
            immune.setEid(immune.getEid());
            immuneService.save(immune);
            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }

    /**
     * 修改
     * @param immune
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public ReturnVO edit(@RequestBody Immune immune) {
        try {
            immuneService.updateById(immune);
            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }

    /**
     * 删除
     * @param immune
     * @return
     */
    @PostMapping("/del")
    @ResponseBody
    public ReturnVO del(@RequestBody Immune immune) {
        try {
            immuneService.removeById(immune.getImmuneID());
            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }
}
